<?php
namespace Site\Controller;
use Think\Controller;

class CommonController extends Controller {
    // 布局模板
    protected $layout = 'default';

    protected $sid = 0;

    public function __construct() {
        parent::__construct();

        set_exception_handler(array($this,'exception_error'));

        if(in_array(strtolower(CONTROLLER_NAME . '.' . ACTION_NAME),C('WHITE_AUTH'))) {
            return;
        }

        $username = session('username');
        if(!$username) {
            $this->redirect('/user/signin');
            exit();
        }

        $sid = session('sid');
        $where = $sid ? "`id`={$sid}" : "";
        $siteInfo = M('Site')->where($where)->field('id as sid,title as sitename,domain as host,tid')->find();
        if($siteInfo) {
            C($siteInfo);
            C('domain',"http://{$siteInfo['host']}/");
            $this->sid = $siteInfo['sid'];
        }
        if(IS_POST) {
            $_POST['sid'] = $this->sid;
        }
    }

    public function display($templateFile = '') {
        $this->assign(array(
            'action_name'       =>  strtolower(ACTION_NAME),
            'controller_name'   =>  strtolower(CONTROLLER_NAME),
            'level_one_title'   =>  $this->level_one_title,
            'level_two_title'   =>  $this->level_two_title,
            'front_icon'        =>  $this->front_icon,
        ));

        if($this->layout === false) {
            parent::display($templateFile);
        }else {
            $container = $this->fetch($templateFile);

            $this->assign('container',$container);

            parent::display('Public:' . $this->layout);
        }
        exit();
    }

    protected function success($message,$redirect_url = '') {
        session('web_message',$message);
        session('web_message_code',200);

        if(!$redirect_url) $redirect_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/';
        redirect($redirect_url);exit();
    }

    protected function successAjax($data,$message = '') {
        echo json_encode(array(
            'code'      =>  200,
            'data'      =>  $data,
            'message'   => $message,
        ));exit();
    }

    protected function error($message,$redirect_url = '') {
        session('web_message',$message);
        session('web_message_code',0);

        if(!$redirect_url) $redirect_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/';
        redirect($redirect_url);exit();
    }
    
    protected function errorAjax($message) {
        echo json_encode(array(
            'code'      =>  0,
            'message'   =>  $message,
        ));exit();
    }

    public function exception_error(\Exception $e) {
        if(IS_AJAX) {
            $this->errorAjax($e->getMessage());
        }else{
            $this->error($e->getMessage());
        }
    }

    protected function getPageList($Model,$where = array(),$order = 'id DESC',$size = 12) {

        $count  = $Model->where($where)->count();// 查询满足要求的总记录数

        $Page = new \Think\Page($count,$size);

        $page = $Page->show();

        $list = $Model->limit($Page->firstRow . ',' . $Page->listRows)->where($where)->order($order)->select();

        $data = array(
            'list'  =>  $list,
            'pager'  =>  $page,
            'count' =>  $count,
            'size'  =>  $size,
        );

        $this->assign($data);

        return $data;
    }

    protected function setTitle($level_one_title,$level_two_title = '',$front_icon = '') {
       $this->level_one_title   =  $level_one_title;
       $this->level_two_title   =  $level_two_title;
       $this->front_icon        =  $front_icon;
    }
}
?>
